<template>
  <div>
    <MyTable :list="list">
      <template #head>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{ row }">
        <td>{{ row.id }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>
          <!-- 焦点，enter添加，esc清空 -->
          <input
            v-if="row.inputVisible"
            class="tag-input form-control"
            style="width: 100px"
            type="text"
            v-fff
            v-model="row.inputValue"
            @blur="row.inputVisible = false"
            @keydown.enter="
              row.inputValue.trim().length !== 0 &&
                row.tags.push(row.inputValue),
                (row.inputValue = '')
            "
            @keydown.esc="row.inputValue = ''"
          />
          <!-- 点击改 true -->
          <button
            v-else
            style="display: block"
            class="btn btn-primary btn-sm add-tag"
            @click="row.inputVisible = true"
          >
            +Tag
          </button>
          <!-- 渲染tags -->
          <span
            class="badge badge-warning"
            v-for="(str, ind) in row.tags"
            :key="ind"
            >{{ str }}</span
          >
        </td>
        <td>
          <button class="btn btn-danger btn-sm" @click="delFn(row.id)">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from '../components/MyTable'

export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    }
  },
  //发请求
  created() {
    this.$axios({
      url: '/api/goods',
    })
      .then((res) => (this.list = res.data.data))
      .catch((err) => console.log(err))
  },
  //过滤删除
  methods: {
    delFn(id) {
      this.list = this.list.filter((item) => item.id !== id)
    },
  },
  //自定义焦点
  directives: {
    fff: {
      inserted(el) {
        el.focus()
      },
    },
  },
}
</script>
<style lang="less" scoped>
.my-goods-list {
  .badge {
    margin-right: 5px;
  }
}
</style>
